package com.enqualcomm.kids.activities;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.bean.TerminalBillMessage;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.terminalbill.BillPresenter;
import com.enqualcomm.kids.mvp.terminalbill.CheckBillMessageAdapter;
import com.enqualcomm.kids.mvp.terminalbill.ICheckBillView;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;

import java.util.List;

import common.utils.PromptUtil;


public class CheckBillActivity extends BaseActivity implements ICheckBillView, View.OnClickListener {

    TerminallistResult.Terminal terminal;

    BillPresenter presenter;
    String terminalid;

    CheckBillMessageAdapter adapter;

    ListView listView;
    LinearLayout no_message_show_ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_bill);
        terminal = getTerminal();
        initTitle();  //初始化标题
        initView();

        presenter = new BillPresenter(this, terminal.terminalid,terminal.userterminalid);
        adapter = new CheckBillMessageAdapter(this, presenter);
        listView.setAdapter(adapter);
        presenter.attachView(this);


    }

    private void initView() {

        listView = (ListView) findViewById(R.id.checkbill_listview);
        no_message_show_ll = (LinearLayout) findViewById(R.id.no_message_show_ll);
        findViewById(R.id.title_bar_title_tv).setOnClickListener(this);
        findViewById(R.id.title_bar_left_iv).setOnClickListener(this);
        findViewById(R.id.bill_hbt).setOnClickListener(this);
        findViewById(R.id.bill_lbt).setOnClickListener(this);
        findViewById(R.id.title_bar_settings_iv).setOnClickListener(this);
        findViewById(R.id.title_bar_delete_sms_iv).setOnClickListener(this);
    }

    private void initTitle() {
        QueryUserTerminalInfoResult.Data info = new UserTerminalDefault(terminal.userterminalid).getInfo();
        setupTitleIcon(info,terminal.terminalid, terminal.userterminalid, getString(R.string.check_bill_title));
    }

    @Override
    public void showMessage() {
        PromptUtil.toast(this, getString(R.string.check_watch_bill_too_many_times));
    }


    @Override
    public void notifyAdapterDataChange(List<TerminalBillMessage> messageList) {
        adapter.setData(messageList);
        adapter.notifyDataSetChanged();
        listView.smoothScrollToPosition(messageList.size() - 1);
    }

    @Override
    public void showNoMessageBackground(boolean isToShow) {
        if (isToShow) {
            no_message_show_ll.setVisibility(View.VISIBLE);
        } else {
            no_message_show_ll.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.onStop();

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.title_bar_title_tv:
            case R.id.title_bar_left_iv:
                finish();
                break;
            case R.id.bill_hbt:
                presenter.checkTermianlBill(0);
                break;
            case R.id.bill_lbt:
                presenter.checkTermianlBill(1);
                break;
            case R.id.title_bar_settings_iv:
                startActivityWithTerminal(SetCheckBillSettingsActivity_.class, terminal);
                break;
            case R.id.title_bar_delete_sms_iv:
                presenter.deleteAllMessage();
                break;
        }
    }


}
